<!--教师管理员报名管理页面-->
<template>
    <div>
        <div style="display: flex;align-items: center;justify-content: center">
            <el-input style="width: 150px" placeholder="查询名称" v-model="name" clearable></el-input>

            <el-button type="primary" style="margin-left: 10px" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" plain>重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-button type="danger" plain @click="delBatch" v-if="user.role === '管理员'">批量删除</el-button>
        </div>
        <el-table :data="tableData"
                  stripe
                  :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666'}"
                  style="font-size: 14px; margin-top: 20px"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <el-table-column prop="name" label="竞赛名称" align="center"></el-table-column>
            <el-table-column prop="payNo" label="支付流水号" align="center"></el-table-column>
            <el-table-column prop="money" label="支付金额" align="center"></el-table-column>
            <el-table-column prop="buyer" label="购买人" align="center"></el-table-column>
            <el-table-column prop="payTime" label="支付时间" align="center"></el-table-column>
           
            <el-table-column label="操作" align="center" width="220" v-if="user.role === '管理员'">
                <template v-slot="scope" style="font-size: 14px">
                    <el-button style="padding: 6px" size="mini" type="danger" plain @click="del(scope.row.id)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0; display: flex; align-items: center; justify-content: center">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "PayManage",
        data() {
            return {
                tableData: [], //所有的数据
                pageNum: 1, //当前的页码
                pageSize: 6, //每页显示的个数
                name: '',
                total: 0, //默认是0
                user: JSON.parse(localStorage.getItem("honey-user") || '{}'),//拿取个人用户信息
                ids: []
            }
        },
        created() {
            this.load()
        },
        methods: {
            delBatch() { //批量删除
                if (!this.ids.length) {
                    this.$message.warning('请选择数据')
                    return
                }
                this.$confirm('确认批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
                    this.$request.delete('/pay/delete/batch', {
                        data: this.ids
                    }).then(res => {
                        if (res.code === '200') { //操作成功
                            this.$message.success('操作成功')
                            this.load(1) //刷新表格数据
                        } else { //操作失败
                            this.$message.error(res.msg) //弹出错误信息
                        }
                    })
                }).catch(() => {
                })
            },
            handleSelectionChange(rows) { //选中的数据 rows:当前选中的所有行数据
                this.ids = rows.map(v => v.id)
            },
            del(id) { //单个删除
                this.$confirm('确认删除？', '确认删除', {type: "warning"}).then(response => {
                    this.$request.delete('/pay/delete/' + id).then(res => {
                        if (res.code === '200') { //操作成功
                            this.$message.success('操作成功')
                            this.load(1) //刷新表格数据
                        } else { //操作失败
                            this.$message.error(res.msg) //弹出错误信息
                        }
                    })
                }).catch(() => {
                })
            },
            reset() { //重置
                this.name = ''
                this.load()
            },
            load(pageNum) { //分页查询
                if (pageNum) {
                    this.pageNum = pageNum
                }
                this.$request.get('/pay/selectByPage', {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.name,
                    }
                }).then(res => {
                    this.tableData = res.data.records
                    console.log(this.tableData)
                    this.total = res.data.total
                })
            },
            handleCurrentChange(pageNum) {
                this.load(pageNum)
            },
        }
    }

</script>

<style>

</style>